﻿using Newtonsoft.Json;
using SM23;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestItems.Model;
using TlToolBase;
using TlToolBase.Mode;

namespace TlToolCordCreate.Web
{
    /// <summary>
    /// 网络请求
    /// </summary>
    public class MyWebRequest
    {
        HttpHelp _httpHelp { get; set; }
        HttpResultValue _token { get; set; }
        /// <summary>
        /// 服务地址
        /// </summary>
        public string ServerIP { get; set; } = "127.0.0.1";
        /// <summary>
        /// 服务端口
        /// </summary>
        public int ServerPort { get; set; } = 5005;

        /// <summary>
        /// 获取tonek地址
        /// </summary>
        private string apiGetToenk = "http://{0}:{1}/api/sysAuth/login";
        /// <summary>
        /// 添加表
        /// </summary>
        private string apiAddTable = "http://{0}:{1}/api/sysDatabase/addTable";
        /// <summary>
        /// 添加代码
        /// </summary>
        private string apiAddCodeGen = "http://{0}:{1}/api/sysCodeGen/add";
        /// <summary>
        /// 创建实体
        /// </summary>
        private string apiCreateEntity = "http://{0}:{1}/api/sysDatabase/createEntity";
        /// <summary>
        /// 按业务名称创建代码
        /// </summary>
        private string apiRunLocalBusName = "http://{0}:{1}/api/sysCodeGen/runLocalBusName";
        /// <summary>
        /// 添加mes任务
        /// </summary>
        private string apiAddOrder = "http://{0}:{1}/api/mesOrdreSrever/order";

        public MyWebRequest()
        {
            _httpHelp = new HttpHelp();
        }

        /// <summary>
        /// 添加表
        /// </summary>
        [Obsolete]
        public void AddTable(DbTableInput input)
        {
            Task.Factory.StartNew(() =>
            {
                var url = string.Format(apiGetToenk, ServerIP, ServerPort);
                var dst = _httpHelp.Post(url, input, _token.result.accessToken);
                dst.Wait(1000);
                try
                {
                    var res = JsonConvert.DeserializeObject<HttpResultValue>(dst.Result);
                    if (res.code == 200)
                    {
                        //action?.Invoke(true);
                    }
                }
                catch (Exception ex)
                {

                }
            });
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="action"></param>
        public void GetToken(Action<bool> action)
        {
            Task.Factory.StartNew(() =>
            {
                var url = string.Format(apiGetToenk, ServerIP, ServerPort);
                var dst = _httpHelp.Post(url, new LoginInput()
                {
                    Account = "admin",
                    Password = SM2Encryption.Encrypt("123456"),
                    Code = "string",
                    CodeId = 0
                });
                dst.Wait(1000);
                try
                {
                    _token = JsonConvert.DeserializeObject<HttpResultValue>(dst.Result);
                    if (_token.code == 200)
                    {
                        action?.Invoke(true);
                    }
                }
                catch (Exception ex)
                {

                }
            });
        }

        /// <summary>
        /// 添加mes任务
        /// </summary>
        /// <param name="input"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void AddMesTask(MesOrdreInput input)
        {
            Task.Factory.StartNew(() =>
            {
                var url = string.Format(apiAddOrder, ServerIP, ServerPort);
                var dst = _httpHelp.Post(url, input, _token.result.accessToken);
                dst.Wait(1000);
                try
                {
                    var resultValue = JsonConvert.DeserializeObject<HttpResultValue>(dst.Result);
                    if (resultValue.code == 200)
                    {
                        //action?.Invoke(true);
                    }
                }
                catch (Exception ex)
                {

                }
            });
        }
    }
}
